/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mundialfp.view.pais;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import mundialfp.view.ResponseManager;
import mundialfp.view.main.Principal;
import mundialfp.view.tables.ranking.RankingTableModel;
import mundialfp.view.ws.PaisWSService;
import uy.com.pzit.core.util.ImageUtil;
import uy.com.pzit.fpmundialmodel.web.ws.Pais;
import uy.com.pzit.fpmundialmodel.web.ws.PaisResponse;

/**
 *
 * @author pferraz
 */
public class RankingManager {

    private final RankingView main;

    public RankingManager(RankingView rankingView) {
        this.main = rankingView;
    }

    public void cargando(boolean cargando) {
        main.getLblBallLoad().setVisible(cargando);
        main.getLblLoad().setVisible(cargando);
    }

    public void getRankingFIFA() {
        cargando(true);
        main.enableComponents(main.getPnlRankingFIFA(), false);
        new NonBlockingOperationPrincipalManager("getRankingFIFA").start();
    }

    private class NonBlockingOperationPrincipalManager extends Thread {

        private final String methodName;

        public NonBlockingOperationPrincipalManager(String methodName) {
            this.methodName = methodName;
        }

        @Override
        public void run() {
            try {
                if (this.methodName.equalsIgnoreCase("getRankingFIFA")) {
                    getRankingFIFA();
                }
                this.interrupt();
            } catch (Throwable ex) {
                Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        private void getRankingFIFA() {
            main.gettGenerator().createRankingTable(main.getTblListaPais(), main.getPnlTable(), main.getTxtPaisQuickSearch(),main.getLblLoad(),main.getLblBallLoad());
            PaisResponse resp = PaisWSService.getPaisesOrderByRanking();
            if (ResponseManager.validateResponse(resp)) {
                RankingTableModel dTM = new RankingTableModel(0);
                main.getTblListaPais().setModel(dTM);
                for (Pais p : resp.getPaises()) {
                    Object[] newRow = {dTM.getRowCount() + 1,new ImageIcon(ImageUtil.resizeImage(ImageUtil.decodeToImage(p.getBanderaB64()), main.getBANDERA_WIDTH(), main.getBANDERA_HEIGHT())), p.getNombre(),p.getRankingFifa()};
                    dTM.addRow(newRow);
                }
                main.getTblListaPais().resizeColumns();   
                main.getPaisTableSorter().initQuickSearch(main.getTblListaPais(), main.getTxtPaisQuickSearch(), main.getBANDERA_HEIGHT() + 5,2);
            }
            cargando(false);
            main.getPnlTable().setVisible(true);
        }

    }
}
